var TraningPoint = {
	listInit: function(){ 
	   	TraningPoint.trainpointJump(1);
	},
	trainpointClick: function(pageclickednumber){
		var cont = GetLS("cont");
      	TraningPoint.trainpointJump(pageclickednumber);
      	$("#pager").pager({pagenumber: pageclickednumber, pagecount: cont, buttonClickCallback: TraningPoint.trainpointClick });
	},
	trainpointJump: function(pageno){
		var count = Res.sinCount;
      	var start = (pageno-1)*count;
		$ajax({
			url: "/trainpoint/list",
			data:{
				start: start,
				count: count
			},
			success: function(res) {
				var cont = parseInt(res.result.count/Res.sinCount);
                if((res.result.count)%Res.sinCount!=0){
                     cont++;
                }
                SetLS("cont",cont);
                if($("#pager").children().length == 0)
                     $("#pager").pager({ pagenumber: pageno, pagecount: cont, buttonClickCallback: TraningPoint.trainpointClick });
				var html = '<thead><tr>'+
                            '<th class="span4">名称</th>'+  
                            '<th class="span4">地址</th>'+
                            '<th class="span2">所在地</th>'+
                            '<th class="span2">方向</th>'+
                            '<th class="span2">操作</th>'+
	                    '</tr></thead><tbody>';

	            var items = res.result.results;
	            for (var i=0; i<items.length; i++) {
	            	var btnHtml = '<button class="btn btn-sm updateBtn">修改</button> <button class="btn btn-danger btn-sm removeBtn">删除</button>';
	            	html += '<tr data-pos="'+ i +'">'+
	            				'<td>'+ items[i].name +'</td>'+ 
	            				'<td>'+ items[i].addr +'</td>'+ 
	            				'<td>'+ items[i].province  +  items[i].city +'</td>'+ 
	            				'<td>'+ (items[i].direction?items[i].direction.name:"") +'</td>'+ 
	            				'<td>'+ btnHtml +'</td>'+
	            			'</tr>';

	            }
	            html += '</tbody>';

	            $("#tabCtn").html(html);
	            $(".removeBtn").click(function() {
		            var item = items[$(this).parents("tr").data("pos")];
		        	if(window.confirm('你确定要删除培训点“' +  item.name + '”吗？删除后将不能恢复！')){
		            	$ajax({
		            		url:"/trainpoint/remove",
		            		data:{_id:item._id},
		            		success: function() {
		            			window.location.href = "/trainpointList.html";
		            		}
		            	});
		          	}
	            });
	           	$(".updateBtn").click(function() {
	            	var item = items[$(this).parents("tr").data("pos")];
	            	window.location.href = "/trainpointCreate.html?id="+item._id;
	            });
			}
		});	       
	},
	createInit: function() {
		$("#create").click(TraningPoint.add);
		$ajax({
			url:"/direction/list",
			success:function(res){
				var directions = res.result.results;
				var html = '';
				for(var i=0;i < directions.length;i++){
					html +=' <option value="'+ directions[i]._id +'">' + directions[i].name+ '</option>';
				}
				$("#direction").append(html);
				var id = getValue('id');
				if(id) {
					$ajax({
						url: "/trainpoint/detail",
						data: {
							_id: id,
						},
						success: function(res) {
							var direction = res.result;
							$("#name").val(direction.name);
							$("#addr").val(direction.addr);
							$("#province").val(direction.province);
							addCity();
							$("#city").val(direction.city);
							$("#contact_name").val(direction.contact.name);
							$("#tel").val(direction.contact.tel);
							$("#direction").val(direction.direction);
							//$("#direction option[value='"+direction.direction+"']").attr("selected","selected");			
								}
					});			
				}
			}
		});
	},

	add: function(d) {
		var name = $("#name").val();
		var addr = $("#addr").val();
		var province = $("#province").val();
		var city = $("#city").val();
		var contact = new Object;
		contact.name = $("#contact_name").val();
		contact.tel = $("#tel").val();
		var direction = $("#direction").val();
		if (name == "") {
			return $alert("培训点名称不能为空");
		};
		if (addr == "") {
			return $alert("地址不能为空");
		};
		if (province == "0" || city == "0") {
			return $alert("请选择所在地");
		};
		if (contact.name == "") {
			return $alert("联系人姓名不能为空");
		};
		if (contact.tel == "") {
			return $alert("电话不能为空");
		};
		if (direction == "") {
			return $alert("培训方向不能为空");
		};
		
		var id = getValue("id");
		var data = {};
		data.name = name;
		data.addr = addr;
		data.province = province;
		data.city = city;
		data.contact = contact;
		data.direction = direction;		
		if(id)
			data._id = id;
		$ajax({
			url: "/trainpoint/upsert",
			data: data,
			success: function(res) {
				window.location.href = "/trainpointList.html";
			}
		});
	},

}
